
package de.swta.freezay.database.wicketAdapter;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Package;
import org.apache.wicket.model.LoadableDetachableModel;

/**
 * A LoadableDetachableModel for package entities
 *
 * @author Christian Baeumer
 * @see LoadableDetachableModel
 */
public class DetachablePackageModel extends LoadableDetachableModel<Package> {

    /** the package id of this package model */
    private int packageID;

    public DetachablePackageModel(final Package p) {
        this(p.getId());
    }


    public DetachablePackageModel(final int id) {
        packageID = id;
    }

    @Override
    protected Package load() {
        /** loads package from database */
        return JPAController.PackageJpaController.findPackage(packageID);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + packageID;
        return hash;
    }

    /**
     * used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
     *
     * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj)
    {
        if (obj == null) {
            return false;
        } else if (obj == this) {
            return true;
        } else if (obj instanceof DetachablePackageModel) {
            DetachablePackageModel other = (DetachablePackageModel) obj;
            return other.packageID == packageID;
        }

        return false;
    }

}
